package com.zzy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.common.State;
import com.zzy.common.Successful;
import com.zzy.common.ZZYUtils;
import com.zzy.entity.Essay;
import com.zzy.entity.User;
import com.zzy.mapper.UserMapper;
import com.zzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    public Successful<String> login(String username, String password, HttpServletResponse response){
        Successful<String> successful = new Successful<>();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password",password);
        User user = userService.queryUser(wrapper);
        if (user == null){
            successful.setState(State.ERROR);
            successful.setMessage("用户不存在");
        }else {
            successful.setState(State.SUCCEED);
            successful.setMessage("登录成功");
            successful.setData(user.getUsername());
            response.addCookie(ZZYUtils.addCookies("user", user.getUsername()));
            response.addCookie(ZZYUtils.addCookies("uid", String.valueOf(user.getUId())));
        }
        return successful;
    }

    @PostMapping("/register")
    public Successful<String> register(User u){
        System.out.println(u);
        Successful<String> successful = new Successful<>();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",u.getUsername());
        User user = userService.queryUser(wrapper);
        if (user == null){
//            userService.saveUser(u);
            userMapper.insert(u);
            successful.setState(State.SUCCEED);
            successful.setMessage("创建用户成功");
            successful.setData(u.getUsername());
        }else {
            successful.setState(State.ERROR);
            successful.setMessage("用户存在 请重试");
            successful.setData(null);
        }

        return successful;
    }

    @GetMapping("/exit")
    public Successful<String> exit(HttpServletRequest req){
        ZZYUtils.exitCookies(req);
        Successful<String> successful = new Successful<>(State.SUCCEED,"请求成功","退出账户成功");
        return  successful;
    }

    @GetMapping("/getUser")
    public Successful<User> getUser(@CookieValue("uid") Integer uid){
        User user = userMapper.selectById(uid);
        return new Successful<User>(State.SUCCEED,"用户",user);
    }

    @GetMapping("/getUserInfo")
    public Successful<User> getUserInfo(@CookieValue("uid") Integer uid){
        System.out.println(uid);
        User user = userMapper.selectById(uid);
        System.out.println(user);
        return new Successful<User>(State.SUCCEED,"查询成功",user);
    }

    @PutMapping("/updateUserIndo")
    public Successful<User> updateUserIndo(@CookieValue("uid") Integer uid,User user){
        System.out.println(user);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",uid);
        userMapper.update(user,queryWrapper);
        return new Successful<User>(State.SUCCEED,"修改成功",user);
    }

    @PostMapping("/uploadHead")
    public Successful<String> uploadHead(@CookieValue("uid") Integer uid, MultipartFile file,HttpServletRequest request) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //获取后缀名
        String[] split = originalFilename.split("\\.");
        String pre = split[split.length - 1];

        File file1 = ZZYUtils.createMedioFile(userMapper.selectById(uid).getUsername() + "headimage."+pre);
        file.transferTo(file1);

        int serverPort = request.getServerPort();
        String IP = InetAddress.getLocalHost().getHostAddress();
        String medioRemotePath = "http://" + IP + ":" + serverPort + "/static" + "/" + userMapper.selectById(uid).getUsername() + "headimage."+pre;
        System.out.println(medioRemotePath);
        userMapper.updateHead(medioRemotePath,uid);
        return new Successful<String>(State.SUCCEED,"上传成功",medioRemotePath);

    }

    @GetMapping("/admin/allUser")
    public Successful<List<User>> userManagement(){
        List<User> users = userMapper.selectList(null);
        return new Successful<List<User>>(State.SUCCEED,"所有用户查找成功",users);
    }

    @DeleteMapping("/admin/deleteuser/{uid}")
    public Successful<Integer> deleteUserById(@PathVariable Integer uid){
        System.out.println(uid);

        int i = userMapper.deleteById(uid);
        return new Successful<>(State.SUCCEED,"删除用户成功",i);
    }




}
